package org.macpro.alone.server.common.database;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.macpro.alone.prisist.common.LimitQueryForPageInter;
import org.macpro.alone.server.domain.PageDomain;
import org.macpro.alone.server.domain.SqlDomain;

/** 条件分页等复杂查询
 * @author 王东
 * 
 */
public class DataComplexQueryManager implements DataComplexQueryManagerInter {
	private LimitQueryForPageInter limitQueryDao;

	/* (non-Javadoc)
	 * @see org.macpro.alone.server.common.database.DataComplexQueryManagerInter#runComplexQuery(org.macpro.alone.server.domain.PageDomain, java.lang.Object, org.macpro.alone.server.domain.SqlDomain)
	 */
	public PageDomain runComplexQuery(PageDomain pagepara, Object Class,
			SqlDomain sqldomain) {
		
		limitQueryDao.InitPara(pagepara.getFirstResourt(), pagepara
				.getSizeResourt());
		pagepara.resourtList = limitQueryDao.runLimitQuery(sqldomain
				.getSql_part(), Class);
		pagepara.setTotalResourtSize(limitQueryDao.runLimitQueryNum(sqldomain
				.getSql_part()));
		return pagepara;
	}
	/* (non-Javadoc)
	 * @see org.macpro.alone.server.common.database.DataComplexQueryManagerInter#runComplexQuery(org.macpro.alone.server.domain.PageDomain, java.lang.String, org.macpro.alone.server.domain.SqlDomain)
	 */
	@SuppressWarnings("unchecked")
	public PageDomain runComplexQuery(PageDomain pagepara,String sql,SqlDomain sqldomain){
		StringBuffer SQL_run=new StringBuffer().append("select ").append(sql).append(sqldomain.getSql_part());
		String upsql=SQL_run.toString().toUpperCase();
		String partSql=upsql.substring(upsql.indexOf("FROM"));
		String headsql=upsql.substring(0,upsql.indexOf("FROM"));
		if(headsql.indexOf("*")>=0){
			throw new RuntimeException("select can't contain *");
		}
		limitQueryDao.InitPara(pagepara.getFirstResourt(), pagepara
				.getSizeResourt());
		//将一个pageDomain中的list<object>解析为map<String,String>
		List<Map<String, String>> limap=this.parseToListTypeMapForStringArray(limitQueryDao.runLimitQuery(upsql), upsql);
		pagepara.resourtList = (List) limap;
		pagepara.setTotalResourtSize(limitQueryDao.runLimitQueryNum(partSql));
		return pagepara;
	}
	private List<Map<String, String>> parseToListTypeMapForStringArray(List<Object> obj,String sql){
		String sql_=sql;
		sql_=sql_.toUpperCase();
		sql_=sql_.substring(0,sql_.indexOf("FROM"));
		sql_=sql_.replace("SELECT","");
		String[] title_=sql_.split(",");
		List<Object> list=obj;
		List<Map<String, String>> maplist=new ArrayList<Map<String,String>>();
		for (Iterator<Object> iterator = list.iterator(); iterator.hasNext();) {
			Object[] name = (Object[]) iterator.next();
			Map<String,String> map=new HashMap<String, String>();
			for (int i = 0; i < name.length; i++) {
				String tatget=(String)name[i];
				map.put(title_[i].trim(),tatget);
			}
			maplist.add(map);
		}
		return maplist;
	}
	public LimitQueryForPageInter getLimitQueryDao() {
		return limitQueryDao;
	}
	public void setLimitQueryDao(LimitQueryForPageInter limitQueryDao) {
		this.limitQueryDao = limitQueryDao;
	}
}
